

//
//  StockPopVIewController.m
//  ProjectforHDX
//
//  Created by motom on 2016/11/22.
//  Copyright © 2016年 H.DX. All rights reserved.
//

#import "StockPopVIewController.h"
#import "StockPopViewCell.h"

@interface StockPopVIewController ()<UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate>


@end

@implementation StockPopVIewController
{
    UINib *nib;
    NSMutableArray *popArr;
    NSMutableArray *saveData;
    NSMutableArray *IDarr;
    UITableView *tableView;
    NSMutableArray *groupArr;  //组数组
    NSMutableArray *lineArr;   //行数组
    NSMutableArray *regionArr;
    NSMutableArray *popCodeArr;
    NSString *seratStr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"%@",_teger);
    [self setupView];

}


- (void)setupView
{
    
    popArr = [NSMutableArray array];
    popCodeArr = [NSMutableArray array];
    groupArr = [NSMutableArray array];
    lineArr = [NSMutableArray array];
    regionArr = [NSMutableArray array];
   
    if ([_teger isEqualToString:@"2"]) {
        for (int i=0; i<_popViewArr.count; i++) {
            [groupArr addObject:_popViewArr[i][@"iProvince"]];
            regionArr = _popViewArr[i][@"region"];

           [popArr addObject:regionArr];

            for (int j = 0 ; j < regionArr.count; j++) {
                
                [popCodeArr addObject:regionArr[j][@"storeName"]];
            }
        }
    }else {
        for (int i=0; i<_popViewArr.count; i++) {
            [popArr addObject:_popViewArr[i][@"storeName"]];
        }
    }
    
    if ([_teger isEqualToString:@"1"]) {
        
        CGFloat cellWidth = 136;
        
        UIImageView *backgroundImage = [[UIImageView alloc]init];
        backgroundImage.image = BDFILEIMAGENAEM(@"Home/img_popView");
        
        [self.view addSubview:backgroundImage];
        
        [backgroundImage setFrame:CGRectMake(0, 0, 191, 147)];
        
        tableView = [[UITableView alloc]init];
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.view addSubview:tableView];
        [tableView setFrame:CGRectMake(5, 10, 180, 127)];
        tableView.backgroundColor = [UtilCheck hexStringToColor:@"505558" alpha:1.0];
    }else {
        
        CGFloat cellWidth = 136;
        
        UIImageView *backgroundImage = [[UIImageView alloc]init];
        backgroundImage.image = BDFILEIMAGENAEM(@"Home/img_popView");
        [self.view addSubview:backgroundImage];
        [backgroundImage setFrame:CGRectMake(0, 0, 191, 227)];
        
        UISearchBar *searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(5, 15, 180, 40)];
        searchBar.backgroundColor = [UIColor blackColor];
        searchBar.placeholder = [UtilCheck language:@"setting.stockPlate"];
        [searchBar setDelegate:self];
        [self.view addSubview:searchBar];
        if ([_teger isEqualToString:@"2"]) {
            saveData = [[NSMutableArray alloc]initWithArray:popCodeArr];
            IDarr = [[NSMutableArray alloc]initWithArray:popCodeArr];
        }else {
            saveData = [[NSMutableArray alloc]initWithArray:popArr];
            IDarr = [[NSMutableArray alloc]initWithArray:popArr];
        }

        tableView = [[UITableView alloc]init];
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.view addSubview:tableView];
        [tableView setFrame:CGRectMake(5, 55, 180, 167)];
        tableView.backgroundColor = [UtilCheck hexStringToColor:@"505558" alpha:1.0];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



#pragma mark - Table view data source
//组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if ([_teger isEqualToString:@"2"]) {
        return groupArr.count;
    }
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if ([_teger isEqualToString:@"1"] ) {
        return 3;
    }else if ([_teger isEqualToString:@"2"]) {
        NSLog(@"%@",popArr[section]);
        NSArray *array = [NSArray array];
        array = popArr[section];
        return array.count;
    }else if ([_teger isEqualToString:@"100"]) {
        return popCodeArr.count;
    }
    else{
        return popArr.count;
    }
    
}
//头部
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if ([_teger isEqualToString:@"2"]) {
        return groupArr[section];
    }else {
        return 0;
    }
    
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([_teger isEqualToString:@"1"]) {
        static NSString *identify = @"StockPopViewCell";
        if (!nib) {
            nib = [UINib nibWithNibName:@"StockPopViewCell" bundle:nil];
            [tableView registerNib:nib forCellReuseIdentifier:identify];
        }
        StockPopViewCell *popCell = [tableView dequeueReusableCellWithIdentifier:identify];
        popCell.selectionStyle = UITableViewCellSeparatorStyleNone;
        switch (indexPath.row) {
            case 0:
                popCell.cellImage.image = BDFILEIMAGENAEM(@"Home/img_fixPart");
                popCell.cellName.text = [UtilCheck language:@"order.search.fix"];
                break;
            case 1:
                popCell.cellImage.image = BDFILEIMAGENAEM(@"Home/img_faultPart");
                popCell.cellName.text = [UtilCheck language:@"order.search.fault"];
                break;
            case 2:
                popCell.cellImage.image = BDFILEIMAGENAEM(@"Home/img_all");
                popCell.cellName.text = [UtilCheck language:@"order.search.all"];
                break;
            default:
                break;
        }
        
        return popCell;
    } if([_teger isEqualToString:@"2"]) {
        static NSString *str=@"cell";
        UITableViewCell *viewCell=[tableView dequeueReusableCellWithIdentifier:str];
        if (viewCell ==nil) {
            viewCell=[[UITableViewCell alloc]initWithStyle: UITableViewCellStyleDefault reuseIdentifier:str];
        }
        viewCell.textLabel.text = popArr[indexPath.section][indexPath.row][@"storeName"];
        viewCell.textLabel.textColor = [UIColor whiteColor];
        viewCell.textLabel.textAlignment = UITextAlignmentCenter;
        viewCell.backgroundColor = [UtilCheck hexStringToColor:@"505558" alpha:0.1];
        return viewCell;
    }else {
        static NSString *str=@"cell";
        UITableViewCell *viewCell=[tableView dequeueReusableCellWithIdentifier:str];
        if (viewCell ==nil) {
            viewCell=[[UITableViewCell alloc]initWithStyle: UITableViewCellStyleDefault reuseIdentifier:str];
        }
        if ([_teger isEqualToString:@"100"]) {
            viewCell.textLabel.text = popCodeArr[indexPath.row];
            viewCell.textLabel.textColor = [UIColor blackColor];
        }else {
            viewCell.textLabel.text = popArr[indexPath.row];
            viewCell.textLabel.textColor = [UIColor whiteColor];
        }
        viewCell.textLabel.textAlignment = UITextAlignmentCenter;
        viewCell.backgroundColor = [UtilCheck hexStringToColor:@"505558" alpha:0.1];
        return viewCell;
    }
    

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([_teger isEqualToString:@"1"]) {
        if ([self.delegate respondsToSelector:@selector(didSelectPopCell:)]) {
            [self.delegate didSelectPopCell:indexPath.row];
        }
    }else if ([_teger isEqualToString:@"100"]) {
        if ([self.delegate respondsToSelector:@selector(didSelectPopCellTwo:setArr:strTager:)]) {
            [self.delegate didSelectPopCellTwo:indexPath.row setArr:popCodeArr strTager:@"1"];
        }
        if ([self.delegate respondsToSelector:@selector(didSelectPopCellTwo:setCodeArr:strTager:)]) {
            [self.delegate didSelectPopCellTwo:indexPath.row setCodeArr:popCodeArr strTager:@"1"];
        }

    }else if ([_teger isEqualToString:@"2"]) {
        if ([self.delegate respondsToSelector:@selector(didSelectPopCellTwo:setArr:strTager:)]) {
            [self.delegate didSelectPopCellTwo:indexPath.row setArr:popArr[indexPath.section] strTager:@"0"];
        }
        if ([self.delegate respondsToSelector:@selector(didSelectPopCellTwo:setCodeArr:strTager:)]) {
            [self.delegate didSelectPopCellTwo:indexPath.row setCodeArr:popArr[indexPath.section] strTager:@"0"];
        }

    }else {
        if ([self.delegate respondsToSelector:@selector(didSelectPopCellTwo:setArr:)]) {
            [self.delegate didSelectPopCellTwo:indexPath.row setArr:popArr];
        }
    }
    

}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    if ([_teger isEqualToString:@"2"]) {
        if (searchText.length>0)
        {
            for (int i=0; i<searchText.length; i++)
            {
                NSRange range=NSMakeRange(i,1);
                NSString *subString=[searchText substringWithRange:range];
                const char *cString=[subString UTF8String];
                //判断searchBar.text是中文还是字母
                //中文
                if (strlen(cString)==3)
                {
                    NSMutableArray *content = [NSMutableArray array];
                    [popCodeArr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
                        [content addObject:obj];
                    }];
                    NSPredicate *pre = [NSPredicate predicateWithFormat:@"self contains %@",searchText];
                    //arr1是搜索结果数据
                    NSMutableArray *arr1 = [[NSMutableArray alloc] initWithArray:[content filteredArrayUsingPredicate:pre]];
                    //临时数组
                    NSMutableArray *tempArr = [NSMutableArray  array];
                    [arr1 enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
                        for (int i=0; i<saveData.count; i++)
                        {
                            if ([saveData[i] isEqualToString:obj]) {
                                [tempArr addObject:saveData[i]];
                            }
                        }
                    }];
                    popCodeArr = tempArr;
                    //字母
                }else if(strlen(cString)==1)
                {
                    searchText=[searchText lowercaseString];
                    NSMutableArray *content = [NSMutableArray array];
                    [IDarr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
                        [content addObject:obj];
                    }];
                    NSPredicate *pre = [NSPredicate predicateWithFormat:@"self contains %@",searchText];
                    //arr1是搜索结果数据
                    NSMutableArray *arr1 = [[NSMutableArray alloc] initWithArray:[content filteredArrayUsingPredicate:pre]];
                    //临时数组
                    NSMutableArray *tempArr = [NSMutableArray  array];
                    [arr1 enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
                        for (int i=0; i<IDarr.count; i++)
                        {
                            if ([IDarr[i] isEqualToString:obj]) {
                                [tempArr addObject:IDarr[i+1]];
                            }
                        }
                    }];
                    popCodeArr = tempArr;
                }
            }
        }else{
            popCodeArr = saveData;
        }
         _teger = @"100";
    }else {
        if (searchText.length>0)
        {
            for (int i=0; i<searchText.length; i++)
            {
                NSRange range=NSMakeRange(i,1);
                NSString *subString=[searchText substringWithRange:range];
                const char *cString=[subString UTF8String];
                //判断searchBar.text是中文还是字母
                //中文
                if (strlen(cString)==3)
                {
                    NSMutableArray *content = [NSMutableArray array];
                    [popArr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
                        [content addObject:obj];
                    }];
                    NSPredicate *pre = [NSPredicate predicateWithFormat:@"self contains %@",searchText];
                    //arr1是搜索结果数据
                    NSMutableArray *arr1 = [[NSMutableArray alloc] initWithArray:[content filteredArrayUsingPredicate:pre]];
                    //临时数组
                    NSMutableArray *tempArr = [NSMutableArray  array];
                    [arr1 enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
                        for (int i=0; i<saveData.count; i++)
                        {
                            if ([saveData[i] isEqualToString:obj]) {
                                [tempArr addObject:saveData[i]];
                            }
                        }
                    }];
                    popArr = tempArr;
                    //字母
                }else if(strlen(cString)==1)
                {
                    searchText=[searchText lowercaseString];
                    NSMutableArray *content = [NSMutableArray array];
                    [IDarr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
                        [content addObject:obj];
                    }];
                    NSPredicate *pre = [NSPredicate predicateWithFormat:@"self contains %@",searchText];
                    //arr1是搜索结果数据
                    NSMutableArray *arr1 = [[NSMutableArray alloc] initWithArray:[content filteredArrayUsingPredicate:pre]];
                    //临时数组
                    NSMutableArray *tempArr = [NSMutableArray  array];
                    [arr1 enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
                        for (int i=0; i<IDarr.count; i++)
                        {
                            if ([IDarr[i] isEqualToString:obj]) {
                                [tempArr addObject:IDarr[i+1]];
                            }
                        }
                    }];
                    popArr = tempArr;
                }
            }
        }else{
            popArr = saveData;
        }
    }
    NSLog(@"%@",popCodeArr);
    [tableView reloadData];
    [self.view endEditing:YES];
}

/*
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:   forIndexPath:indexPath];
    
     Configure the cell...
    
    return cell;
}
*/

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
